Connecting to MariaDB via Python

The following example shows how to connect to the MariaDB server via python.

Install pymysql

pip install pymysql

Connect to your server

import pymysql


class MariadbConnection(object):
    def __init__(self):
        self.host = 'connect host'
        self.port = 4100  # your port
        self.user = 'your user'
        self.passwd = 'user password'
        self.db = 'your connect database name'

    def connect_mariadb(self):
        return pymysql.Connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd)

    def operate_database(self):
        # example select mysql version
        connect = self.connect_mariadb()
        cursor = connect.cursor()
        cursor.execute("SELECT VERSION()")
        # data = cursor.fetchone()
        print(cursor.fetchone()[0].split('-')[0][0:4])
        connect.close()


if _name_ == '__main__':
    MariadbConnection().operate_database()
Copyright © 2025 Cloud Clusters LLCRevised on 05/07/2025

results matching ""

    No results matching ""